package com.task.server.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.task.mapper.TaskMapper;
import com.task.pojo.Task;
import com.task.server.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author JiaFuJun
 * @version v1.0
 * @date 2019-12-13
 */
@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskMapper taskMapper;

    @Override
    public void save(Task record) {
        if (record.getId() == null) {
            taskMapper.insert(record);
        } else {
            taskMapper.update(record);
        }
    }

    @Override
    public void del(Object id) {
        taskMapper.del(id);
    }

    @Override
    public Task selectByPrimaryKey(Object id) {
        return taskMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Task> getList(Task record) {
        return taskMapper.selectByRecord(record);
    }

    @Override
    public PageInfo<Task> pageList(Task record, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<Task> list = taskMapper.selectByRecord(record);
        return new PageInfo<>(list);
    }

    @Override
    public void update(Task record) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (record.getState().equals(1)) {
            record.setActualTime(format.format(new Date()));
        }
        if (record.getState().equals(4)) {
            record.setEndTime(format.format(new Date()));
        }
        taskMapper.update(record);
    }

}